package com.siysoft.tc.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.siysoft.tc.base.bean.bo.AccountBo;
import com.siysoft.tc.base.bean.bo.InterestBo;
import com.siysoft.tc.base.bean.bo.MessageBo;
import com.siysoft.tc.base.bean.bo.PaginationBo;
import com.siysoft.tc.base.bean.bo.PersonalChatBo;
import com.siysoft.tc.base.bean.bo.ReservationBo;
import com.siysoft.tc.base.bean.bo.ReservationDetailBo;
import com.siysoft.tc.base.bean.bo.SystemMessageBo;
import com.siysoft.tc.base.bean.bo.UserBo;
import com.siysoft.tc.base.bean.to.School;
import com.siysoft.tc.base.bean.to.User;
import com.siysoft.tc.base.service.IUserService;
import com.siysoft.util.COMMON;
import com.tc.base.controller.BaseController;

@Controller
@RequestMapping(value = "/app/user")
public class UserController extends BaseController{
	@Autowired
	IUserService userService;
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/interest")
	public void interest(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<InterestBo> interest = userService.getInterest(pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("interest", COMMON.NulltoBlank(interest));
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/teacher_course_list")
	public void teacher_course_list(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					|| COMMON.isnull(request.getParameter("teacher_name"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			String teacher_name = request.getParameter("teacher_name").toString();
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<InterestBo> interest = userService.getTeacherCourseList(pb, teacher_name);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("interest", COMMON.NulltoBlank(interest));
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/toppart")
	public void toppart(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			
			User user = userService.getUserById(id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("user_school", COMMON.NulltoBlank(user.getUser_school()));
			obj.accumulate("user_name", COMMON.NulltoBlank(user.getUser_name()));
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	
	@RequestMapping(value = "/school")
	public void school(HttpServletRequest request,
			HttpServletResponse response){
		try{
			
			List<School> school = userService.getSchool();
			
			JSONObject obj = new JSONObject();
			obj.accumulate("school", COMMON.NulltoBlank(school));
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/info")
	public void info(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("reservation_limit"))
					|| COMMON.isnull(request.getParameter("reservation_offset"))
					|| COMMON.isnull(request.getParameter("assessment_limit"))
					|| COMMON.isnull(request.getParameter("assessment_offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer reservation_limit = Integer.valueOf(request.getParameter("reservation_limit").toString());
			Integer reservation_offset = Integer.valueOf(request.getParameter("reservation_offset").toString());
			Integer assessment_limit = Integer.valueOf(request.getParameter("assessment_limit").toString());
			Integer assessment_offset = Integer.valueOf(request.getParameter("assessment_offset").toString());
			
			PaginationBo reservation_pb = new PaginationBo(reservation_limit, reservation_offset, 0);
			PaginationBo assessment_pb = new PaginationBo(assessment_limit, assessment_offset, 0);
			UserBo user = userService.getUserInfo(id, reservation_pb, assessment_pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("user", user);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/reservation")
	public void reservation(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			
			List<ReservationBo> reservation = userService.getReservation(id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("reservation", COMMON.NulltoBlank(reservation));
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/account")
	public void account(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<AccountBo> account = userService.getAccount(id, pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("account", COMMON.NulltoBlank(account));
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/systemmessage")
	public void systemmessage(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<SystemMessageBo> systemmessage = userService.getSystemMessage(id, pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("systemmessage", COMMON.NulltoBlank(systemmessage));
			obj.accumulate("pagination", pb);
			
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/message")
	public void message(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<MessageBo> message = userService.getMessage(id, pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("message", COMMON.NulltoBlank(message));
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/messagedetail")
	public void messagedetail(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<MessageBo> message = userService.getMessageDetail(id, pb);
			JSONObject obj = new JSONObject();
			obj.accumulate("message", COMMON.NulltoBlank(message));
			obj.accumulate("pagination", pb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/reply")
	public void reply(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("reciver_id"))
					|| COMMON.isnull(request.getParameter("message"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer reciver_id = Integer.valueOf(request.getParameter("reciver_id").toString());
			String message = request.getParameter("message").toString();
			Integer result = userService.reply(id, reciver_id, message);
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/reservationdetail")
	public void reservationdetail(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("user_id"))
					|| COMMON.isnull(request.getParameter("reservation_id"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer user_id = Integer.valueOf(request.getParameter("user_id").toString());
			Integer reservation_id = Integer.valueOf(request.getParameter("reservation_id").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			ReservationDetailBo reservationdetail = userService.getReservationDetail(user_id, reservation_id, pb);
			Integer user_reservation_status = userService.getUserReservationStatus(user_id, reservation_id);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("reservationdetail", reservationdetail);
			obj.accumulate("user_reservation_status", user_reservation_status);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/personal_chat_detail")
	public void personal_chat_detail(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("user_id1"))
					|| COMMON.isnull(request.getParameter("user_id2"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer user_id1 = Integer.valueOf(request.getParameter("user_id1").toString());
			Integer user_id2 = Integer.valueOf(request.getParameter("user_id2").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<PersonalChatBo> pcb = userService.getPersonalChatDetail(user_id1, user_id2, pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("personal_chat", pcb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/personal_chat_send")
	public void personal_chat_send(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("sender_id"))
					|| COMMON.isnull(request.getParameter("receiver_id"))
					|| COMMON.isnull(request.getParameter("content"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer sender_id = Integer.valueOf(request.getParameter("sender_id").toString());
			Integer receiver_id = Integer.valueOf(request.getParameter("receiver_id").toString());
			String  content = request.getParameter("content").toString();
			
			String result = userService.PersonalChatSend(sender_id, receiver_id, content);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/personal_chat_list")
	public void personal_chat_list(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("limit"))
					|| COMMON.isnull(request.getParameter("offset"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			Integer limit = Integer.valueOf(request.getParameter("limit").toString());
			Integer offset = Integer.valueOf(request.getParameter("offset").toString());
			PaginationBo pb = new PaginationBo(limit, offset, 0);
			List<PersonalChatBo> pcb = userService.PersonalChatList(id, pb);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("personal_chat", pcb);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
}
